﻿using System;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;

namespace YJKIndicateDimension.DrawLaneLine
{
	public class LaneLineXDataManager
	{
		public static bool WriteXData(Element ele, string strValue)
		{
			bool result = false;
			try
			{
				LaneLineXDataManager.Instance();
				Entity entity = LaneLineXDataManager.m_xdataMgr.CreateEntity();
                LaneLineXDataManager.m_xdataMgr.SetElementEntity<string>(ele, LaneLineXDataManager.m_strFieldName, strValue, (DisplayUnitType)(-2), ref entity);
				LaneLineXDataManager.m_xdataMgr.SetElementXData(ele, entity);
				result = true;
			}
			catch
			{
			}
			return result;
		}

		public static string ReadXData(Element ele)
		{
			string result = string.Empty;
			try
			{
                result = XDataManager.GetElementXData<string>(LaneLineXDataManager.m_Guid, ele, LaneLineXDataManager.m_strFieldName, (DisplayUnitType)(-2));
			}
			catch
			{
			}
			return result;
		}

		private static XDataManager Instance()
		{
			if (LaneLineXDataManager.m_xdataMgr == null)
			{
				SchemaManager schemaManager = new SchemaManager(new SchemaData(LaneLineXDataManager.m_Guid, LaneLineXDataManager.m_schemaName, ""));
                schemaManager.AddField<string>(LaneLineXDataManager.m_strFieldName, (UnitType)(-2));
				LaneLineXDataManager.m_xdataMgr = new XDataManager(schemaManager);
				LaneLineXDataManager.m_xdataMgr.FinishSchema();
			}
			return LaneLineXDataManager.m_xdataMgr;
		}

		private static Guid m_Guid = new Guid("AE3D6D8D-A736-4AB8-8669-4DD9E822FCAB");

		private static string m_schemaName = "LaneLineXData";

		private static string m_strFieldName = "LineMark";

		private static XDataManager m_xdataMgr = null;
	}
}
